﻿using System;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;

namespace CloundUploader.Framework.Upload
{
   public class Ku6UploadUploader : BaseFileUploader
    {
        public const string WebCode = "ku6";
        string Boundary = "-----------------------------1122439710187";
        string HeadBoundary = "---------------------------1122439710187";
        public byte[] HeadBytes;
        public byte[] EndBytes;
        public string FileUploadId { get; set; }
        public UploaderContext Context { get; set; }
        string HeadData;
        private HttpWebRequest UploadRequest { get; set; }
        public UploadFileInfo UploadFile { get; set; }
        public Ku6UploadUploader(UploaderContext context, UploadFileInfo fileInfo)
        {
            Context = context;
            UploadFile = fileInfo;
        }
        public override void Initial()
        {
        }
        public override  void CreatVideoUploadId()
        {
            WebClient wc = new WebClient();
            wc.Headers.Add(Context.User.GetWebUser(WebCode).Cookie);
            wc.Headers.Add("Referer: http://www.pomoho.com/upload/");
            wc.Headers.Add("Content-Type: application/x-www-form-urlencoded");
            wc.Encoding = Encoding.UTF8;
            //filename=&biaoti=%B6%AF%CE%EF%C3%C7%B3%AC%BB%AC%BB%FC%CB%B2%BC%E4%BC%AF%BD%F5&Selectly=%D7%AA%D4%D8&pindao=%B8%E3%D0%A6&pindaoss=1&biaoqian=%B6%AF%CE%EF%C3%C7+%B3%AC%BB%AC%BB%FC+%CB%B2%BC%E4%BC%AF%BD%F5&jianjie=%B6%AF%CE%EF%C3%C7%B3%AC%BB%AC%BB%FC%CB%B2%BC%E4%BC%AF%BD%F5&sc=%A1%A1%CF%C2%D2%BB%B2%BD%A1%A1

            string str = wc.DownloadString(new Uri("http://upload.pomoho.com/videoupload/uploadvideo.aspx?param=1%7c%7c0%7cfully%7cwmv%2cmpeg%2cmpg%2cavi%2casf%2crm%2crmvb%2cmp4%2cmov%2c3gp%2cflv%2cdat%2c%7c200"));
            //b605860c-c057-4d14-a9a7-c64f8b259b95
            Regex r = new Regex(".{8}-.{4}-.{4}-.{4}-.{12}");
            Match m = r.Match(str);
            //mtvup.asp?mtvid=113955&ProgressID=
            FileUploadId = m.Value;
            //var userIP = "60.191.250.88";
            r = new Regex("userIP = \"\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
            m = r.Match(str);


        }
        public override void PostHeadData(Stream outStream)
        {
            outStream.Write(HeadBytes, 0, HeadBytes.Length);
        }
        public override void PostEndData(Stream outStream)
        {
            outStream.Write(EndBytes, 0, EndBytes.Length);
        }
        public override Stream CreatStream(long length)
        {
            CreatVideoUploadId();
            string s = Context.User.GetWebUser(WebCode).Cookie;
            var r = new Regex("LoginInfo=\\d{7,9}");
            var m = r.Match(s);

            //http://upload.pomoho.com/videoupload/PostFile.aspx?userIP=115.236.19.24&UserID=10298126&datatime=634343540649552500&uploadID=920784f9-c711-461e-81c5-5bf226abbbe8&random=1298728529422

            //7467368781211187392
            UploadRequest = (HttpWebRequest) HttpWebRequest.Create("http://upload.pomoho.com/videoupload/PostFile.aspx?userIP=" + "&UserID=" + m.Value.Replace("LoginInfo=", "") +  "&uploadID=" + FileUploadId + "&random=1267280671171");
            UploadRequest.AllowWriteStreamBuffering = false;
            // wc = (HttpWebRequest)HttpWebRequest.Create("http://upload.6.cn/upload.php?UPLOAD_METTER_ID=7467368781211187392");
            UploadRequest.ServicePoint.ConnectionLimit = 10;
            UploadRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)";
            UploadRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";

            UploadRequest.Headers.Add("Cache-Control: no-cache");
            UploadRequest.Headers.Add("Accept-Encoding: gzip,deflate");
            UploadRequest.ContentType = "multipart/form-data; boundary=" + HeadBoundary;
            UploadRequest.Referer = "http://upload.pomoho.com/";
            UploadRequest.Headers.Add(Context.User.GetWebUser(WebCode).Cookie);
            UploadRequest.Method = "POST";
            UploadRequest.KeepAlive = true;
            UploadRequest.Timeout = 1800000;
            UploadRequest.ContentLength = HeadBytes.Length + EndBytes.Length + length;
            //  wc.ContentLength =length;
            Stream requestStream = UploadRequest.GetRequestStream();
            return requestStream;
        }
        public override bool GetResponse()
        {
            try
            {
                Thread.Sleep(5000);
                var response = (HttpWebResponse)UploadRequest.GetResponse();

                var responseStream = response.GetResponseStream();
                var responseReader = new StreamReader(responseStream);
                string text = responseReader.ReadToEnd();
                responseStream.Close();
                response.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public override void CreatData()
        {

            string userfile = Boundary + "\r\nContent-Disposition: form-data; name=\"file1\"; filename=\"" + UploadFile.Name  + "\"\r\nContent-Type: video/x-flv\r\n\r\n";
            HeadData = userfile;
            HeadBytes = Encoding.UTF8.GetBytes(HeadData);

            string MAX_FILE_SIZE = "\r\n" + Boundary;
            MAX_FILE_SIZE += Boundary + "\r\nContent-Disposition: form-data; name=\"FileCount\"\r\n\r\n" + "1" + "\r\n";
            MAX_FILE_SIZE += Boundary + "\r\nContent-Disposition: form-data; name=\"hidFileList\"\r\n\r\n" + "" + "\r\n";
            MAX_FILE_SIZE += Boundary + "\r\nContent-Disposition: form-data; name=\"hidVideoNameList\"\r\n\r\n" + "" + "\r\n";
            string end = MAX_FILE_SIZE + Boundary + "--";
            EndBytes = Encoding.UTF8.GetBytes(end);

        }
        public override void PostData()
        {
 
        }

    }

}
